/*
Copyright (C) 2003 Michael Würtinger

This file is part of Dynamicengine.

Dynamicengine is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.

Dynamicengine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Dynamicengine. If not, see http://www.gnu.org/licenses/.
*/


#include "reader.h"


#define STRETCH 2.5

/*
bool CStructFile::ReadFile(char* strFile)
{
	long int lVersion = 2;
	char strFileId[64] = "Structure File for \"Structure Editor\"";


	CFile File;
	if(!File.Open(strFile, CFile::modeRead))
	{
		//MessageBox(NULL, "The specified file could not be opened", "", MB_ICONSTOP);
		return false;
	}

	CArchive ar(&File, CArchive::load);

	char strFileIdCheck[64];
	ar.Read(strFileIdCheck, 64);
	if(strncmp(strFileIdCheck, strFileId, 64) != 0)
	{
		//MessageBox(NULL, "The File is demaged or not an Struct-File!\nThe File cannot be loaded!", "", MB_ICONSTOP);
		return false;
	}

	
	long int lVersionCheck;
	ar.Read(&lVersionCheck, sizeof(lVersionCheck));
	if(lVersionCheck != lVersion)
	{
		//MessageBox(NULL, "Bad File Version!\nThe File cannot be loaded!", "", MB_ICONSTOP);
		return false;
	}

	ar >> m_XSize >> m_YSize;
	ar.Read(&m_bGrid, sizeof(m_bGrid));
	ar >> m_uCurrentTool;
	
	
	// Joints
	
	CStructFileJoint Joint;
	
	UINT j_size;
	ar >> j_size;
	
	for(int i=0; i < j_size; i++)
	{
		if(ar.Read(&Joint, sizeof(Joint)) != sizeof(Joint))
			break;

		m_lJoints.push_front(Joint);
	}


	// Trusses

	CStructFileTruss Truss;
	
	UINT t_size;
	ar >> t_size;

	for(int j=0; j < t_size; j++)
	{
		if(ar.Read(&Truss, sizeof(Truss)) != sizeof(Truss))
			break;

		m_lTrusses.push_front(Truss);
	}

	return true;
}

void CStructFile::Translate(CSite* pSite)
{
	if(!pSite)
		return;

	pSite->Clear();
	
	
	static std::list<CStructFileJoint>::iterator sj_start, sj_end, sj_iter;
	sj_start = m_lJoints.begin();
	sj_end = m_lJoints.end();
	sj_iter = sj_start;

	while(sj_iter != sj_end)
	{
		pSite->CreateJoint(sj_iter->bStatic, vec3f(sj_iter->x*STRETCH, sj_iter->y*STRETCH, 0.0));

		sj_iter++;
	}


	static std::list<CStructFileTruss>::iterator t_start, t_end, t_iter;
	t_start = m_lTrusses.begin();
	t_end = m_lTrusses.end();
	t_iter = t_start;

	CJoint *pJoint1, *pJoint2;

	while(t_iter != t_end)
	{
		pJoint1 = pSite->IsJoint(t_iter->x1*STRETCH, t_iter->y1*STRETCH);
		pJoint2 = pSite->IsJoint(t_iter->x2*STRETCH, t_iter->y2*STRETCH);

		if(pJoint1 && pJoint2)
		{
			pSite->CreateTruss(pJoint1, pJoint2, &mat_steel);
		}

		t_iter++;
	}
}
*/
